package framework.classification.entities;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;



import framework.AnnuaireCocola;
import framework.HierarchyCocola;
import framework.classification.entities.lattice.TreillisI;
import framework.classification.library.AbstractClassfication;
import framework.entity.ACLEntity;

public abstract class ClAbstractEntity {



	protected int[][] table;

	protected String name;

	protected TreillisI lattice;

	protected AbstractClassfication cl;

	private ArrayList<String> entity;

	private ArrayList<String> attributes;

	public ClAbstractEntity(String s, AbstractClassfication cl, TreillisI t) {

		this.cl = cl;
		lattice = t;
		name = s;
		entity = new ArrayList<String>();
		attributes = new ArrayList<String>();

	}

	public abstract void createTableModel();
	public abstract String getType();
	public abstract ACLEntity getACLEntity(String s);
	

	public void initialiseOutputFile(){

		File file = new File(cl.getOuputFile()+getType()+"/");
		if (!file.exists()) file.mkdir();

		file = new File(cl.getOuputFile()+getType()+"/Lattice");
		if (!file.exists()) file.mkdir();

		file = new File(cl.getOuputFile()+getType()+"/Latex");
		if (!file.exists()) file.mkdir();

	}

	public  void computeRelation(){
		lattice.computeTableRelation(this);
	}

	public void computeLattice(){
		lattice.computeLattice(this,getAbstractClassfication().getLatticeGeneration());
	}


	public String getName() {
		return name;
	}

	public HierarchyCocola getHierarchy() {
		return cl.getStructure();
	}



	public int[][] getTable() {
		return table;
	}

	public ArrayList<String> getEntity() {
		return entity;
	}

	public ArrayList<String> getAttribut() {
		return attributes;
	}

	public Boolean isAbstract(String s){
		return lattice.getAbstractReduced(this).containsKey(s);
	}

	public ArrayList<String> getSubstituteElement(String element) {
		String conceptName = null;

		ArrayList<String> result = new ArrayList<String>();
		if (lattice.getRealReduced(this).containsKey(element))
			conceptName = lattice.getRealReduced(this).get(element);
		else if (lattice.getAbstractReduced(this).containsKey(element))
			conceptName = lattice.getAbstractReduced(this).get(element);
		else {
			System.err.println("WARNING:: "+ClAbstractEntity.class+" : "+element+" no find");
			return null;
		}

		ArrayList<String> conceptsubs = lattice.getChildrenConcept(this,conceptName);
	
		if (!conceptsubs.contains(conceptName))
			conceptsubs.add(0,conceptName);
		
		for (String sub : conceptsubs){
			if (lattice.getConceptToReduced(this).containsKey(sub))
				for (String el : lattice.getConceptToReduced(this).get(sub))
					result.add(el);
		}

		return result;
	}
	
	public ArrayList<String> getCanbeSubstitutedElement(String element) {
		String conceptName = null;

		ArrayList<String> result = new ArrayList<String>();
		if (lattice.getRealReduced(this).containsKey(element))
			conceptName = lattice.getRealReduced(this).get(element);
		else if (lattice.getAbstractReduced(this).containsKey(element))
			conceptName = lattice.getAbstractReduced(this).get(element);
		else {
			System.err.println("WARNING:: "+ClAbstractEntity.class+" : "+element+" no find");
			return null;
		}

		ArrayList<String> conceptsubs = lattice.getParentConcept(this,conceptName);
		if (!conceptsubs.contains(conceptName))
			conceptsubs.add(0,conceptName);
		
		for (String sub : conceptsubs){
			if (lattice.getConceptToReduced(this).containsKey(sub))
				for (String el : lattice.getConceptToReduced(this).get(sub))
					result.add(el);
		}

		return result;
	}
	
	public ArrayList<String> getConnectedElement(String element){
		String conceptName = null;

		ArrayList<String> result = new ArrayList<String>();
		if (lattice.getRealReduced(this).containsKey(element))
			conceptName = lattice.getRealReduced(this).get(element);
		else if (lattice.getAbstractReduced(this).containsKey(element))
			conceptName = lattice.getAbstractReduced(this).get(element);
		else {
			System.err.println("WARNING:: "+ClAbstractEntity.class+" : "+element+" no find");
			return null;
		}

		ArrayList<String> conceptsubs = lattice.getParentConcept(this,conceptName);
		if (!conceptsubs.contains(conceptName))
			conceptsubs.add(0,conceptName);
		
		for (String sub : conceptsubs){
			if (lattice.getConceptToReduced(this).containsKey(sub))
				for (String el : lattice.getConceptToReduced(this).get(sub))
					result.add(el);
		}

		return result;
	}
	
	public String getPathPDFLattice(){
		return cl.getOuputFile()+getType()+"/Lattice/"+getName()+".pdf";
	}
	public String getPathPNGLattice(){
		return cl.getOuputFile()+getType()+"/Lattice/"+getName()+".png";
	}
	public String getPathSVGLattice(){
		return cl.getOuputFile()+getType()+"/Lattice/"+getName()+".svg";
	}



	public TreillisI getLattice() {
		return lattice;
	}

	public HashMap<String, String> getAbstractReduced() {
		return lattice.getAbstractReduced(this);
	}

	public HashMap<String, String> getRealReduced() {
		return lattice.getRealReduced(this);
	}

	public AbstractClassfication getAbstractClassfication(){return cl;}


}
